﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Use this to generate a simple per pixel filter. 
    /// So all you have to do is to create a simple delegate.
    /// This delegate is used on all pixels then.
    /// </summary>
    public class PerPixelFilterCoordinates : BaseImageFilter
    {
        /// <summary>
        /// 
        /// </summary>
        public delegate Color PerPixelFilterCoordinatesDelegate(int x, int y, Color _color);
        /// <summary>
        /// 
        /// </summary>
        protected PerPixelFilterCoordinatesDelegate m_PerPixelFilterCoordinatesDelegate = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        public PerPixelFilterCoordinates()
        {
            m_PerPixelFilterCoordinatesDelegate = new PerPixelFilterCoordinatesDelegate(
                delegate(int x, int y, Color _color)
                {
                    return ColorFunctions.Gray(_color) > 127 ? Definitions.Black : Definitions.White;
                });
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        /// <param name="_perPixelFilterCoordinatesDelegate">The _per pixel filter coordinates delegate.</param>
        public PerPixelFilterCoordinates(PerPixelFilterCoordinatesDelegate _perPixelFilterCoordinatesDelegate)
        {
            m_PerPixelFilterCoordinatesDelegate = _perPixelFilterCoordinatesDelegate;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerPixelFilter"/> class.
        /// </summary>
        /// <param name="_perPixelFilter">The _per pixel filter.</param>
        internal PerPixelFilterCoordinates(PerPixelFilterCoordinates _perPixelFilter)
        {
            m_PerPixelFilterCoordinatesDelegate = _perPixelFilter.m_PerPixelFilterCoordinatesDelegate;
        }

        /// <summary>
        /// Gets or sets the per pixel filter delegate.
        /// </summary>
        /// <value>The per pixel filter delegate.</value>
        public PerPixelFilterCoordinatesDelegate FilterDelegate
        {
            get { return m_PerPixelFilterCoordinatesDelegate; }
            set { m_PerPixelFilterCoordinatesDelegate = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            Color color = Color.Transparent;
            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    _bitmap.SetPixel(x, y, m_PerPixelFilterCoordinatesDelegate(x, y, _bitmap.GetPixel(x, y)));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new PerPixelFilterCoordinates(this);
        }
    }
}
